/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class TCPPacketSocketCommunicator.
//
// TCPPacketSocketCommunicator uses to send/receive TCP packet data. Each
// send will be a packet, and each receive will be a packet, this packet is a whole
// message in the application layer.
//

#include "tcp_packet_socket_communicator.h"
#include "packet_define.h"
#include "logging.h"

namespace amz {

TCPPacketSocketCommunicator::TCPPacketSocketCommunicator(int socket,
       CompressType compress, EncryptType encrypt)
       : TCPSocketCommunicator(socket, compress, encrypt),
         packet_receive_(packet::kMaxPacketSize), packet_send_(packet::kMaxPacketSize) {}

TCPPacketSocketCommunicator::~TCPPacketSocketCommunicator() {}

int TCPPacketSocketCommunicator::Receive(char *message, int max_size) {
  if(this->CheckAvailable() == true) {
    if(this->packet_receive_.PreWrite(this->packet_receive_.GetUnitBufferSize()) == false) {
      LOG(ERROR) << "Failed to Receive, packet_receive_ size is not enough!";
      return -1;
    }
    int size = recv(this->socket_, this->packet_receive_.WritePointer(),
           this->packet_receive_.WriteSize(), MSG_NOSIGNAL);
    if(size == 0) {
      // Socket shutdown, set unavailable.
      this->SetUnavailable();
    } else if(size > 0) {
      this->packet_receive_.WriteShift(size);
    }
  }

  // Unpacket a message.
  int read_size = this->packet_.Unpacket(this->packet_receive_.ReadPointer(),
         this->packet_receive_.ReadSize(), message, max_size);
  if(read_size > 0) {
    this->packet_receive_.ReadShift(read_size);
  }

  return read_size;
}

int TCPPacketSocketCommunicator::Send(char *message, int size) {
  // Calculate packet size, allocate, packet.
  int need_size = this->packet_.CalculatePacketSize(size);
  if(this->packet_send_.PreWrite(need_size) == false) {
    LOG(ERROR) << "Failed to Send, packet_send_ size is not enough!";
    return -1;
  }
  int packet_size = this->packet_.Packet(message, size, this->packet_send_.WritePointer(),
         this->packet_send_.WriteSize());
  if(packet_size <= 0) {
    return packet_size;
  }
  this->packet_send_.WriteShift(packet_size);

  // Real send to socket.
  int send_size = send(this->socket_, this->packet_send_.ReadPointer(),
         this->packet_send_.ReadSize(), MSG_NOSIGNAL);
  if(send_size < 0) {
    LOG(ERROR) << "Failed to send";
    return -1;
  }
  // Shift buffer.
  this->packet_send_.ReadShift(send_size);

  return send_size;
}

}  // namespace amz

